package com.lu.adog.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.util.Log;

public class NetAddressUitl {
	
	public static String getHostIp() {	
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
						.hasMoreElements();) {
					InetAddress inetAddress = ipAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {						
						return inetAddress.getHostAddress();
					}
				}
			}
		} catch (SocketException ex) {
		} catch (Exception e) {
		}
		return null;
	}

	public static byte[] getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				if (intf.getName().equals("usbnet0")) {
					continue;
				}
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()
							&& (inetAddress instanceof Inet4Address)) {
						// String x = inetAddress.getHostAddress();
						return inetAddress.getAddress();

					}
				}
			}
		} catch (SocketException ex) {
			Log.e(NetAddressUitl.class.getSimpleName(), ex.toString());
		}
		return null;
	}
	/**
	 * 获取特定网络接口的IP，such as eth0,wlan0?
	 * @param name
	 * @return
	 */
	public static byte[] getLocalIpAddress(String name){
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				if (intf.getName().equals(name)) {
					for (Enumeration<InetAddress> enumIpAddr = intf
							.getInetAddresses(); enumIpAddr.hasMoreElements();) {
						InetAddress inetAddress = enumIpAddr.nextElement();
						if (!inetAddress.isLoopbackAddress()
								&& (inetAddress instanceof Inet4Address)) {
							return inetAddress.getAddress();

						}
					}
				}
			}
		} catch (SocketException ex) {
			Log.e(NetAddressUitl.class.getSimpleName(), ex.toString());
		}
		return null;

	}
	public static byte[] getMacAddress() {
		Enumeration<NetworkInterface> interfaces = null;
		try {
			interfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while (interfaces.hasMoreElements()) {
			final NetworkInterface ni = interfaces.nextElement();
			try {
				if (ni.isLoopback() || ni.isPointToPoint() || ni.isVirtual())
					continue;
			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			byte[] macAddress = null;
			try {
				macAddress = ni.getHardwareAddress();
			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (macAddress != null && macAddress.length > 0)
				return macAddress;
		}
		return null;
	}
}
